package com.esms.esmsadmin.config;

import com.esms.esmsadmin.entity.AdminEntity;
import com.esms.esmsadmin.entity.RespBean;
import com.esms.esmsadmin.service.AdminService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.*;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.PrintWriter;

/**
 * @author chenhong
 * @version 1.0.0
 * @ClassName SecurityConfig.java
 */

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired(required = false)
    AdminService adminService;

   /* @Resource
    CustomFilterInvocationSecurityMetadataSource customFilterInvocationSecurityMetadataSource;*/

    //    @Resource
//    CustomUrlDecisionManager customUrlDecisionManager;
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(adminService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/login");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .and()
                .formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/doLogin")
                .loginPage("/login")
                .successHandler((request, response, authentication) -> {
                    response.setContentType("application/json;charset=utf-8");
                    PrintWriter printWriter = response.getWriter();
                    AdminEntity admin = (AdminEntity) authentication.getPrincipal();
                    admin.setPassword(null);
                    RespBean ok = RespBean.ok("登录成功", admin);
                    String s = new ObjectMapper().writeValueAsString(ok);
                    printWriter.write(s);
                    printWriter.flush();
                    printWriter.close();
                })
                .failureHandler((request, response, exception) -> {
                    response.setContentType("application/json;charset=utf-8");
                    PrintWriter printWriter = response.getWriter();
                    RespBean respBean = RespBean.error("登录失败");
                    if (exception instanceof LockedException) {
                        respBean.setMsg("账户被锁定,请联系管理员");
                    } else if (exception instanceof CredentialsExpiredException) {
                        respBean.setMsg("密码过期,请联系管理员");
                    } else if (exception instanceof AccountExpiredException) {
                        respBean.setMsg("账户过期,请联系管理员");
                    } else if (exception instanceof DisabledException) {
                        respBean.setMsg("账户被禁用,请联系管理员");
                    } else if (exception instanceof BadCredentialsException) {
                        respBean.setMsg("用户名或者密码输入错误,请重新输入");
                    }
                    String s = new ObjectMapper().writeValueAsString(respBean);
                    printWriter.write(s);
                    printWriter.flush();
                    printWriter.close();
                })
                .permitAll()
                .and()
                .logout()
                .logoutSuccessHandler((request, response, authentication) -> {
                    response.setContentType("application/json;charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(RespBean.ok("注销成功！")));
                    out.flush();
                    out.close();
                })
                .permitAll()
                .and()
                .csrf().disable()
//                没有认证时在这处理
                .exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
                    response.setContentType("application/json;charset=utf-8");
                    response.setStatus(401);
                    PrintWriter printWriter = response.getWriter();
                    RespBean respBean = RespBean.error("访问失败");
                    if (authException instanceof InsufficientAuthenticationException) {
                        respBean.setMsg("非法请求");
                    }
                    String s = new ObjectMapper().writeValueAsString(respBean);
                    printWriter.write(s);
                    printWriter.flush();
                    printWriter.close();
                });
    }
}
